package com.yaxin.mq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public static final String EXCHANGE_NAME = "topic_exchange";

    // 路由键
    public static final String ROUTING_KEY_CREATE = "recruitment.publish.create";
    public static final String ROUTING_KEY_UPDATE = "recruitment.publish.update";
    public static final String ROUTING_KEY_DELETE = "recruitment.publish.delete";
    public static final String ROUTING_KEY_SUBMISSION = "recruitment.submission";
    public static final String ROUTING_KEY_OUTPERSONS = "uat.person";

    // 队列名
    public static final String QUEUE_CREATE = "recruitment_publish_create_queue";
    public static final String QUEUE_UPDATE = "recruitment_publish_update_queue";
    public static final String QUEUE_DELETE = "recruitment_publish_delete_queue";
    public static final String QUEUE_SUBMISSION = "recruitment_submission_queue";
    public static final String QUEUE_OUTPERSONS = "uat_person_queue";
    /**
     * Topic 交换机
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    /**
     * 创建 create 队列
     */
    @Bean
    public Queue createQueue() {
        return new Queue(QUEUE_CREATE, true); // true表示持久化
    }

    /**
     * 创建 update 队列
     */
    @Bean
    public Queue updateQueue() {
        return new Queue(QUEUE_UPDATE, true);
    }

    /**
     * 创建 delete 队列
     */
    @Bean
    public Queue deleteQueue() {
        return new Queue(QUEUE_DELETE, true);
    }

    /**
     * 创建 外协人员 队列
     */
    @Bean
    public Queue outPersonsQueue() {
        return new Queue(QUEUE_OUTPERSONS, true);
    }


    /**
     * 创建 简历投递 队列
     */
    @Bean
    public Queue submissionQueue() {
        return new Queue(QUEUE_SUBMISSION, true);
    }

    /**
     * 绑定 create 路由
     */
    @Bean
    public Binding createBinding(Queue createQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(createQueue).to(topicExchange).with(ROUTING_KEY_CREATE);
    }

    /**
     * 绑定 update 路由
     */
    @Bean
    public Binding updateBinding(Queue updateQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(updateQueue).to(topicExchange).with(ROUTING_KEY_UPDATE);
    }


    /**
     * 绑定 delete 路由
     */
    @Bean
    public Binding deleteBinding(Queue deleteQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(deleteQueue).to(topicExchange).with(ROUTING_KEY_DELETE);
    }

    /**
     * 绑定 简历投递 路由
     */
    @Bean
    public Binding submissionBinding(Queue submissionQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(submissionQueue).to(topicExchange).with(ROUTING_KEY_SUBMISSION);
    }

    /**
     * 绑定 外协人员 队列
     */
    @Bean
    public Binding outPersonsBinding(Queue outPersonsQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(outPersonsQueue).to(topicExchange).with(ROUTING_KEY_OUTPERSONS);
    }

}
